﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MuffinBreak.Bll;

public partial class Templates_Store_Default : Acms.AcmsControls.TemplatePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
        }
    }

    protected void txtAddress_DataBound(object sender, EventArgs e)
    {
        txtAddress.Text = txtAddress.Text.Replace("\n", "<br/>");
    }

    protected void btnShowForm_Click(object sender, EventArgs e)
    {
        phStart.Visible = false;
        phVoteForm.Visible = true;
    }


    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
            MuffinBreakDataContext context = new MuffinBreakDataContext();
            if (MuffinBreak.Bll.Vote.HasVoted(context, txtEmail.Text))
            {
                phAlreadyVoted.Visible = true;
                phVoteForm.Visible = false;
            }
            else
            {
                Vote vote = new Vote();
                vote.PostGuid = AcmsContext.PostGuid;
                vote.EmailAddress = txtEmail.Text;
                vote.FirstName = txtFirstName.Text;
                vote.LastName = txtLastName.Text;
                vote.AllowUpdates = !chkUpdates.Checked;
                vote.ContactTel = txtContactTel.Text;
                
                context.Votes.InsertOnSubmit(vote);
                context.SubmitChanges();

                phVoteForm.Visible = false;
                phAlreadyVoted.Visible = false;
                phVoted.Visible = true;
                lblVoteAverage.Text = Vote.GetPercentageByPostGuid(context, AcmsContext.PostGuid).ToString("0%");
            }
        }
    }

    protected void btnTransfer_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
            MuffinBreakDataContext context = new MuffinBreakDataContext();
            Vote vote = Vote.GetByEmailAddress(context, txtEmail.Text);
            if (vote != null)
            {
                vote.PostGuid = AcmsContext.PostGuid;
                vote.EmailAddress = txtEmail.Text;
                vote.FirstName = txtFirstName.Text;
                vote.LastName = txtLastName.Text;
                vote.ContactTel = txtContactTel.Text;
                vote.AllowUpdates = !chkUpdates.Checked;
                context.SubmitChanges();

                phVoteForm.Visible = false;
                phAlreadyVoted.Visible = false;
                phVoted.Visible = true;
                lblVoteAverage.Text = Vote.GetPercentageByPostGuid(context, AcmsContext.PostGuid).ToString("0%");
            }
        }
    }


}
